30 void copyFrom(
const List& other);
34 List(
const T* elements,
unsigned elementsCount);
38 void add(
const T& element);
40 void insertAt(
const T& element,
unsigned index);
59 std::istream&
read(std::istream& istr);
61 std::ostream&
write(std::ostream& ostr)
const;
88 length = (length == 0) ? 8 : length << 1;
89 T* temp =
new T[length];
90 for (
int i = 0; i < count; i++)
91 temp[i] = elements[i];
103 elements =
new T[other.length];
104 for (
int i = 0; i < other.count; i++)
105 elements[i] = other.elements[i];
106 length = other.length;
115 count = elementsCount;
116 this->elements =
new T[length];
117 for (
unsigned i = 0; i < count; i++)
118 this->elements[i] = elements[i];
123 this->length = count = length;
124 this->elements =
new T[length];
125 for (
unsigned i = 0; i < count; i++)
126 this->elements[i] = elements[i];
134 if (length == count) resize();
136 elements[count++] = element;
147 if (index >= count) {
151 if (length == count) resize();
153 for (
unsigned i = count; i > index; i--)
154 elements[i] = elements[i - 1];
156 elements[index] = element;
167 if (index >= count)
return false;
169 for (
int i = index; i < count; i++)
170 elements[i] = elements[i + 1];
183 while (ind < count && elements[ind].compare(element) != 0)
195 return findIndex(element) < count;
200 return elements[index];
205 return elements[index];
218 this->elements =
new T[length];
223 for (
unsigned i = 0; i < other.length; i++)
239 istr.read((
char*)&length,
sizeof(length));
240 istr.read((
char*)&count,
sizeof(count));
243 elements =
new T[length];
245 for (
int i = 0; i < count; i++)
246 elements[i].
read(istr);
261 ostr.write((
const char*)&length,
sizeof(length));
262 ostr.write((
const char*)&count,
sizeof(count));
264 for (
int i = 0; i < count; i++)
265 elements[i].write(ostr);
293 if (
this != &other) {
314 length = other.length;
316 elements = other.elements;
317 other.elements =
nullptr;
322 if (
this != &other) {
324 length = other.length;
326 elements = other.elements;
327 other.elements =
nullptr;
348 for (
int i = 0; i < count; i++) {
365 for (
int i = 0; i < obj.
get_count(); i++)
std::istream & operator>>(std::istream &istr, List< T > &obj)
Reads List from stream with >> operator.
Definition: List.hpp:342
std::ostream & operator<<(std::ostream &ostr, const List< T > &obj)
Writes List to stream with << operator.
Definition: List.hpp:363
Templated class that stores an array of elements in dynamic memory.
Definition: List.hpp:22
bool contain(const T &element) const
Definition: List.hpp:194
const T * raw_data() const
Returns raw array of data.
Definition: List.hpp:209
T & operator[](unsigned index)
Returns reference to element at index.
Definition: List.hpp:199
unsigned findIndex(const T &element) const
Finds the index of element.
Definition: List.hpp:181
unsigned get_count() const
Returns the count.
Definition: List.hpp:282
List(unsigned length)
Allocates memory for the given length.
Definition: List.hpp:122
std::istream & read(std::istream &istr)
Reads from stream.
Definition: List.hpp:238
const T & operator[](unsigned index) const
Returns constant reference to element at index.
Definition: List.hpp:204
std::ostream & write(std::ostream &ostr) const
Writes to stream.
Definition: List.hpp:260
List(const T *elements, unsigned elementsCount)
Copies all elements in given array.
Definition: List.hpp:113
unsigned get_length() const
Returns the length.
Definition: List.hpp:274
List< T > & operator+=(const List< T > other)
Appends elements from other list.
Definition: List.hpp:222
void add(const T &element)
Adds an element.
Definition: List.hpp:133
bool removeAt(unsigned index)
Removes element at index.
Definition: List.hpp:166
void clear()
Removes all data.
Definition: List.hpp:214
void insertAt(const T &element, unsigned index)
Inserts element at given index.
Definition: List.hpp:146